<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Admin extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 * 返回管理员账号列表
	 */
	public function index() {
		$this->load->model ( 'admin_model' );
		$data ['admins'] = $this->admin_model->find_all_admins ();
		$data ['title'] = '管理员管理 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'admin/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回添加管理员账号页面
	 */
	public function add() {
		$data ['title'] = '添加管理员 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'admin/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回编辑管理员账号页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'admin_model' );
		$data ['admin'] = $this->admin_model->load ( $id );
		$data ['title'] = '编辑管理员 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'admin/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存管理员账号信息
	 */
	public function save() {
		$admin = array (
				'username' => $this->input->post ( 'username' ),
				'password' => md5($this->input->post ( 'password' ).'jishibao'),//待完善
				'name' => $this->input->post ( 'name' ),
				'cellphone' => $this->input->post ( 'cellphone' ),
				'type' => $this->input->post ( 'type' )
		);
		$this->load->model ( 'admin_model' );
		$adminid=$this->admin_model->create ( $admin );
		if(!empty($adminid)){
			return_json(TRUE,'','添加成功！');
		}else{
			return_json(FALSE,'','添加失败！');
		}
	}
	/*
	 * 更新管理员账号信息
	 */
	public function update() {
		$id = $this->input->post('id');
		$admin = array (
				'name' => $this->input->post ( 'name' ),
				'cellphone' => $this->input->post ( 'cellphone' ),
				'type' => $this->input->post ( 'type' )
		);
		$this->load->model ( 'admin_model' );
		if ($id) {
			if($this->admin_model->update ( $id, $admin )){
				return_json(TRUE,'','修改成功！');
			}else{
				return_json(FALSE,'','信息未改动！');
			}
		} 
	}
	/*
	 * 删除管理员账号
	 */
	public function delete() {
		$id = $this->input->post('id');
		$this->load->model ( 'admin_model' );
		if ($this->admin_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！'); 
		}else{
			return_json(FALSE,'','删除失败！');
		}
	}
}